#include "TTableView.h"

#include <QMouseEvent>

TTableView::TTableView(QWidget *parent) :
    QTableView(parent)
{
}

void TTableView::setViewPortMouseTracking(bool tracking)
{
    viewport()->setMouseTracking(tracking);
    viewport()->installEventFilter(this);
}

void TTableView::setClickedColumns(const QList<int> clickedColumns)
{
    _clickedColumns.clear();
    _clickedColumns.append(clickedColumns);
}

bool TTableView::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == viewport())
    {
        QMouseEvent *me = static_cast<QMouseEvent *>(event);
        QModelIndex index = indexAt(me->pos());
        if (index.isValid() && (_clickedColumns.contains(index.column())))
        {
            if (event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease)
            {
                viewport()->setCursor(Qt::PointingHandCursor);
                return false;
            }
        }
        else if (index.isValid() && index.column() >= 0)
        {
            viewport()->setCursor(Qt::ArrowCursor);
            return false;
        }
        else if (event->type() == QEvent::MouseMove)      /** when not in data range, change cursor */
        {
            viewport()->setCursor(Qt::ArrowCursor);
            return false;
        }
    }
    return QTableView::eventFilter(obj, event);
}
